package com.xsb.aidl

import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.telecom.ConnectionService
import android.view.View
import android.widget.Button
import com.xsb.aidl.service.MyService

class MainActivity : AppCompatActivity() {
  lateinit var myBinder:IDataInterface
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    var conn = object : ServiceConnection{
      override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
         myBinder = IDataInterface.Stub.asInterface(service)
      }

      override fun onServiceDisconnected(name: ComponentName?) {
      }
    }
    var intent = Intent("MyService")
    intent.setPackage("com.xsb.aidl")
    findViewById<Button>(R.id.btn_start).setOnClickListener{bindService(intent,conn, Service.BIND_AUTO_CREATE).apply {
    }}

    findViewById<Button>(R.id.btn_end).setOnClickListener{
      unbindService(conn)
    }
    findViewById<Button>(R.id.btn_add).setOnClickListener{
      myBinder.addNum(2,8)
    }
  }
}